package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Classes;
import com.itheima.service.ClassesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * The type Classes controller.
 */
@RestController
@Slf4j
@RequestMapping("/classes")
public class ClassesController {

    @Autowired
    private ClassesService classesService;


    /**
     * 新增班级
     *
     * @param classes the classes
     * @return the result
     */
    @PostMapping
    public Result<?> addClass(@RequestBody Classes classes) {
        classesService.addClass(classes);
        return Result.success();
    }

    /**
     * 通过分页进行查询
     *
     * @param page     the page
     * @param pageSize the page size
     * @param name     the name
     * @param begin    the start time
     * @param end      the end time
     * @return the result
     */
    @GetMapping
    public Result<PageBean<Classes>> selectClassesByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "1") Integer pageSize,
            String name,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {

        PageBean<Classes> pageBean = classesService.selectClassesByPage(page, pageSize, name, begin, end);
        return Result.success(pageBean);


    }

    /**
     * 根据id删除班级
     *
     * @param id the id
     * @return the result
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteClassById(@PathVariable Integer id) {
        classesService.deleteClassById(id);
        return Result.success();
    }

    /**
     * 根据id查询班级信息
     *
     * @param id the id
     * @return the result
     */
    @GetMapping("/{id}")
    public Result<Classes> selectClassById(@PathVariable Integer id) {
        Classes classes = classesService.selectClassById(id);
        return Result.success(classes);
    }

    /**
     * 根据id修改班级信息
     *
     * @param classes the classes
     * @return the result
     */
    @PutMapping
    public Result<?> updateClassById(@RequestBody Classes classes){
        classesService.updateClassById(classes);
        return Result.success();
    }


}
